import {showWarningDialog} from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
import { toast,ajax,broadcast } from "nc-lightapp-front";
import { buttonController } from '../viewController/index';
import { URL, PAGECODE, FIELD, STATUS, TRANSFER, OrderCache, PUSHCONST } from '../../constance';
import { showResumeModal } from 'scmpub/scmpub/components/ResumeModal';
import {
	getDefData,
	addCacheData,
	updateCacheData,
	setDefData,
	rewriteTransferSrcBids
} from '../../../../../scmpub/scmpub/pub/cache';
import { getLangByResId } from '../../../../../scmpub/scmpub/pub/tool/multiLangUtil';
import { showSuccessInfo, showWarningInfo } from '../../../../../scmpub/scmpub/pub/tool/messageUtil';
let _this;

export default function sendMaiiBtnClick(props, skipCodes, callback) {
	if (this) {
		_this = this;
	}
    let sendCount = props.form.getFormItemsValue(PAGECODE.cardhead, FIELD.sendemailcount).value; //累计发送邮箱次数
	let businessemail = props.form.getFormItemsValue(PAGECODE.cardhead, FIELD.businessemail).value;
    if(businessemail==null){
		showWarningDialog(getLangByResId(this, '警告'), getLangByResId(this, '业务Email不能为空'))
		return;
	}
	let msg = ''
	if(!sendCount||sendCount<1){
		msg='确认发送？';
	}else{
		msg='是否再次发送？';
	}
	let bodyids = [ PAGECODE.cardbody, PAGECODE.head_payment ];
	//创建保存的聚合VO createExtCardDataSimple
	let data = props.createExtCardDataSimple(PAGECODE.cardcode, PAGECODE.cardhead, bodyids);	
	if (skipCodes) {
		data['skipCodes'] = skipCodes;
	}
    showWarningDialog('确认发送',  msg, {		
		beSureBtnClick: () => {
			let forderstatus = props.form.getFormItemsValue(PAGECODE.cardhead, FIELD.forderstatus).value; // 单据状态
			if (forderstatus==FIELD.approved) {
				//发送邮件
                ajax({
					method: 'post',
					url: '/nccloud/pu/poordercard/sendmail.do',
					data: data,
					success: function(res) {
						// toast({color:"success",content:"发送成功"})
						// 卡片界面，在浏览态时勾选行，点修改后去掉勾选，付款协议和物料
				props.cardTable.selectAllRows(PAGECODE.head_payment, false);
				props.cardTable.selectAllRows(PAGECODE.cardbody, false);
				if (res.formulamsg && res.formulamsg instanceof Array && res.formulamsg.length > 0) {
					//参数一：返回的公式对象
					props.dealFormulamsg(res.formulamsg);
				}
				// 交互式异常处理
				if (res.data && res.data.isResume && res.data.isResume == true) {
					showResumeModal.call(
						_this,
						props,
						'MessageDlg',
						skipCodes,
						res.data,
						saveButton.bind(this, props, skipCodes, callback),
						props
					);
					return;
				}
				if (res.data) {
					//开关开始
					props.beforeUpdatePage();
					//暂存成功后复位false
					setDefData(OrderCache.OrderCardCache, 'tempsave', false);
					let datas = res.data;
					let pkCache = datas.head.card_head.rows[0].values.pk_order.value;
					if (datas.head) {
						props.form.setAllFormValue({
							[PAGECODE.cardhead]: datas.head[PAGECODE.cardhead]
						});
					}
					let cards = datas.bodys;
					if (cards && cards[PAGECODE.head_payment]) {
						let card_pay = props.cardTable.updateDataByRowId(
							PAGECODE.head_payment,
							cards[PAGECODE.head_payment],
							true
						);
						card_pay.areacode = PAGECODE.head_payment;
						res.data.bodys[PAGECODE.head_payment] = card_pay;
					}
					if (cards && cards[PAGECODE.cardbody]) {
						let card_mater = props.cardTable.updateDataByRowId(
							PAGECODE.cardbody,
							cards[PAGECODE.cardbody],
							true
						);
						card_mater.areacode = PAGECODE.cardbody;
						res.data.bodys[PAGECODE.cardbody] = card_mater;
					}
					props.form.setFormStatus(PAGECODE.cardhead, STATUS.browse);
					//开关关闭
					props.updatePage(PAGECODE.cardhead, [ PAGECODE.head_payment, PAGECODE.cardbody ]);
					let scene = getDefData(OrderCache.OrderCardCache, 'scene');
					props.setUrlParam({ status: STATUS.browse });
					if (scene == null || scene == 'ADD' || scene == 'linksce') {
						//应用场景
						props.setUrlParam({ id: pkCache });
						refreshcache(props, false, datas, pkCache, OrderCache.OrderCacheKey);
					}
				}
				if (res.success) {
					buttonController.materialPasteCancel.call(_this, props);
					buttonController.togglePageShow.call(_this, props, props.getUrlParam(TRANSFER.transfer));
					//2018-11-1;需求（刘兰娇、王丽平）再次变更保存提交给两个提示
					showSuccessInfo(getLangByResId(_this, '发送邮件成功')); /* 国际化处理： 保存成功！*/
					if (callback) {
						callback.call(this, skipCodes);
					}
				}
					}
				});
			} else {
				showWarningDialog('是否成功','发送邮件失败，单据未审核');
			}
		}
	});
}

function refreshcache(props, isAdd, cachedata, pkCache, DS_KEY) {
	if (isAdd) {
		addCacheData(props, FIELD.pk_order, pkCache, cachedata, PAGECODE.cardhead, DS_KEY);
		// 更新翻页组件当前pk值
		props.cardPagination.setCardPaginationId({ id: pkCache, status: 1 });
	} else {
		updateCacheData(props, FIELD.pk_order, pkCache, cachedata, PAGECODE.cardhead, DS_KEY);
	}
}